<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2024-05-23 16:31:14
 * @LastEditors: smallWhite
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /huizhou_website_admin/src/views/project_menagement/components/modal.vue
-->
<template>
  <el-dialog :title="title"
    :visible.sync="dialogVisible"
    width="500px"
    :before-close="handleClose">
    <el-form ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="90px"
      class="demo-ruleForm"
      style="width:100%">
      <el-form-item label="类型 "
          prop="type">
          <el-select style="width: 100%;" v-model="ruleForm.type"
            placeholder="请选择类型">
            <el-option v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="名称"
          prop="name">
        <el-input v-model="ruleForm.name"
          placeholder="请输入名称"></el-input>
      </el-form-item>

      <el-form-item label="链接"
          prop="url">
        <el-input v-model="ruleForm.url"
          placeholder="请输入链接"></el-input>
      </el-form-item>
      <el-form-item
      v-if="ruleForm.type =='2'"
          label="上传图片"
          prop="url">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            name="multipartFile"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img
              v-if="ruleForm.imgUrl"
              :src="ruleForm.imgUrl"
              class="avatar">
            <i v-else
              class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <el-input
          v-if="false"
            v-model="ruleForm.imgUrl"
            placeholder="请上传图片" />
        </el-form-item>
    </el-form>
    <span slot="footer"
      class="dialog-footer">
      <el-button
        @click="resetForm('ruleForm')">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import { HTTP_URI } from '@/api/baseUrl'
export default {
  data() {
    return {
      action: HTTP_URI.UPLOADIMG,
      dialogVisible: false,
      title: '',
      ruleForm: {
        name:'',
        type:1,
        url:'',
        imgUrl:''
      },
      typeList:[
        {
          label:'友情链接',
          value:1
        },
        {
          label:'下属企业',
          value:2
        }
      ],
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        url:[
          { required: true, message: '请输入链接', trigger: 'change' }
        ],
        imgUrl:[
          {required: true, message: '请上传图片', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    open(page,data) {
      if (page === 'add') {
        this.title = '新增'
      } else {
        this.title = '编辑'
        this.ruleForm = data
        console.log( this.ruleForm)
      }
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
      this.ruleForm = {
        name:'',
        type:1,
        url:'',
        imgUrl:''
      }
      this.$refs.ruleForm.resetFields()
      this.$emit('ok')
    },
    handleAvatarSuccess(res, file) {
      console.log(res.data.previewUrl + '/' + res.data.fileName,'0000')
      this.ruleForm.imgUrl = res.data.previewUrl + '/' + res.data.fileName
      console.log(this.ruleForm.imgUrl,'1111')
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isJPG) {
        this.$message.error('图片只能是JPG或者PNG 格式')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },
    getApi(api, data, msg) {
      REQUEST_LIST.REQUST_FCN(api, data, true).then(res => {
        if (res.status === 200) {
          this.$emit('ok')
          this.$message.success(msg)
          this.handleClose()
        }
      })
    },
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
            if (this.title === '新增') {
              this.getApi('PARTYADD', this.ruleForm, '添加成功！')
            } else {
              console.log(this.ruleForm.title)
              const data = {
                id: this.ruleForm.id,
                url: this.ruleForm.url,
                name: this.ruleForm.name,
                type: this.ruleForm.type,
                imgUrl: this.ruleForm.imgUrl,
              }
              this.getApi('PARTYEDIT', data, '编辑成功！')
            }
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
      this.$emit('ok')
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100px;
  height: 100px;
  line-height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
